package com.ysy.tz.mfg;

import com.ysy.tz.core.engine.ApplicationContext;
import com.ysy.tz.core.engine.ConfigLoader;
import com.ysy.tz.core.engine.CustomerFXMLLoader;
import com.ysy.tz.core.selfcomp.MainPage;
import com.ysy.tz.core.utils.FileResourceLoader;
import com.ysy.tz.core.utils.NetWorkUtils;
import com.ysy.tz.core.utils.SQLUtils;
import com.ysy.tz.core.utils.SessionUtils;
import com.ysy.tz.mfg.component.FuncBanner;
import com.ysy.tz.mfg.component.MenuPage;
import com.ysy.tz.mfg.dao.FlightsDao;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.ResourceBundle;

public class MESStarter extends Application {
    private Logger log = LoggerFactory.getLogger(MESStarter.class);

    public MESStarter() {
    }

    public void start(Stage primaryStage) throws Exception {
        Locale.setDefault(Locale.SIMPLIFIED_CHINESE);
        ApplicationContext.commonBundle = ResourceBundle.getBundle("views/i18n/Common");
        this.checkSingleton();
        this.log.debug("----------------------" + ApplicationContext.commonBundle.getString("startInfo") + "-----------------");
        this.log.debug("------" + ApplicationContext.commonBundle.getString("machineInfo") + "------");
        this.log.debug("--hostName: " + NetWorkUtils.getHostName());
        this.log.debug("--ip: " + NetWorkUtils.getIp());
        this.log.debug("--mac: " + NetWorkUtils.getMac());
        this.log.debug("--------------------------------------------------");
        ConfigLoader.load();
        this.initSysConfigFromDB();
        this.showLogin();
        this.initApplicationContext(primaryStage);
        /*SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date lastDate = simpleDateFormat.parse("2023-11-29");
        long duration = lastDate.getTime() - System.currentTimeMillis();
        if (duration < 0L) {
            System.exit(0);
        }*/

        (new Thread(() -> {
            while(true) {
                try {
                    Thread.sleep(10000L);
                } catch (InterruptedException var1) {
                    var1.printStackTrace();
                }

                Runtime.getRuntime().gc();
            }
        })).start();
    }

    private void checkSingleton() {
        /*if (Platform.isWindows()) {
            YsyKernel32 ysyKernel32 = YsyKernel32.INSTANCE;
            int semaphore = ysyKernel32.OpenSemaphore(2031619, true, "ysy-mes");
            if (semaphore == 0) {
                ysyKernel32.CreateSemaphore((WinBase.SECURITY_ATTRIBUTES)null, 1, 1, "ysy-mes");
            } else {
                (new CustomerAlert(AlertType.WARNING, ApplicationContext.commonBundle.getString("running"), new ButtonType[0])).showAndWait();
                System.exit(0);
            }
        }*/
    }

    private void initApplicationContext(Stage primaryStage) {
        ApplicationContext.mainStage = primaryStage;
        ApplicationContext.mainPage = new MainPage();
        ApplicationContext.mainPage.setTopBanner(new FuncBanner());
        ApplicationContext.mainStage.setTitle(ApplicationContext.commonBundle.getString("mesTitle"));
        ApplicationContext.mainStage.setScene(new Scene(ApplicationContext.mainPage));
        ApplicationContext.mainStage.setWidth(1024.0);
        ApplicationContext.mainStage.setHeight(728.0);
        ApplicationContext.mainStage.setOnCloseRequest((windowEvent) -> {
            System.exit(0);
        });
        MenuPage menuPage = new MenuPage();
        ApplicationContext.eventBus.register(menuPage);
        ApplicationContext.mainPage.setCenterPage(menuPage);
        ApplicationContext.mainPage.redirectCenterPage();
    }

    private void showLogin() {
        Dialog dialog = new Dialog();
        ApplicationContext.loginDialog = dialog;
        CustomerFXMLLoader customerFXMLLoader = new CustomerFXMLLoader();

        try {
            customerFXMLLoader.setResources(ResourceBundle.getBundle("views/i18n/Login"));
            InputStream inputStream = FileResourceLoader.loadInputStream("/views/LoginDialog.fxml");
            DialogPane dialogPane = (DialogPane)customerFXMLLoader.load(inputStream);
            inputStream.close();
            dialog.setDialogPane(dialogPane);
            dialogPane.setOnKeyPressed(new EventHandler<KeyEvent>() {
                public void handle(KeyEvent event) {
                    if (KeyCode.ESCAPE == event.getCode()) {
                        System.exit(0);
                    }

                }
            });
            dialogPane.setPrefWidth(Screen.getPrimary().getVisualBounds().getWidth());
            dialogPane.setPrefHeight(Screen.getPrimary().getVisualBounds().getHeight());
            dialog.setTitle(ApplicationContext.commonBundle.getString("mesTitle"));
            dialog.initStyle(StageStyle.TRANSPARENT);
            dialog.show();
        } catch (IOException var5) {
            var5.printStackTrace();
        }

    }

    private void initSysConfigFromDB() {
        SQLUtils.executeSQL(FlightsDao.class, (flightsDao) -> {
            SessionUtils.setAttributes(SessionUtils.FLIGHTS, flightsDao.searchAllFlights());
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}
